package cn.gene.timeyan.security.sso.controller;

import cn.gene.timeyan.common.restful.JsonResult;
import cn.gene.timeyan.common.utils.JwtTokenUtils;
import cn.gene.timeyan.pojo.valid.security.dto.UserCodeLoginDTO;
import cn.gene.timeyan.pojo.valid.security.dto.UserPassswordLoginDTO;
import cn.gene.timeyan.security.sso.pojo.vo.TokenVO;
import cn.gene.timeyan.security.sso.pojo.vo.UserInfoVO;
import cn.gene.timeyan.security.sso.service.IUserInfoService;
import cn.gene.timeyan.security.sso.utils.LoginUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * UserInfoController*
 *
 * @author LWG
 * @date 2022/7/22 15:04
 * @since 1.0.0@gene.cn
 */
@RestController
@Api(tags = "用户信息模块")
@RequestMapping("/user/info/sso")
public class UserInfoController {
    @Resource
    private JwtTokenUtils jwtTokenUtils;
    @Autowired
    private IUserInfoService userInfoService;
    @ApiOperation(value = "前台单点登录认证登出")
    @PostMapping("/logout")
    public JsonResult doLogout(@RequestHeader(name = "Authorization") String token){
        userInfoService.doLogout(token);
        return JsonResult.ok();
    }
    @ApiOperation(value = "用户名密码登录")
    @PostMapping("/login/uname")
    public JsonResult<TokenVO> userNameLogin(@RequestBody UserPassswordLoginDTO userPassswordLoginDTO, HttpServletRequest request){
        System.out.println("开始");
        System.out.println("userPassswordLoginDTO"+userPassswordLoginDTO);
        String ip = LoginUtils.getIpAddress(request);
        String userAgent=request.getHeader("User-Agent");
        userPassswordLoginDTO.setIp(ip);
        userPassswordLoginDTO.setUserAgent(userAgent);
        return userInfoService.userNameLogin(userPassswordLoginDTO);
    }
    @ApiOperation(value = "邮箱或手机号验证登录")
    @PostMapping("/login/verification")
    public JsonResult<TokenVO> verificationLogin(@RequestBody UserCodeLoginDTO userCodeLoginDTO, HttpServletRequest request){
        System.out.println("开始");
        System.out.println("userCodeLoginDTO"+userCodeLoginDTO);
        String ip = LoginUtils.getIpAddress(request);
        String userAgent=request.getHeader("User-Agent");
        userCodeLoginDTO.setIp(ip);
        userCodeLoginDTO.setUserAgent(userAgent);
        return userInfoService.verificationLogin(userCodeLoginDTO);
    }
    @ApiOperation(value = "邮箱或手机号验证登录")
    @GetMapping("/checkToken")
    public JsonResult<Boolean> checkToken(HttpServletRequest request){
        String token = request.getHeader("token");
        Boolean result=jwtTokenUtils.validateToken(token);
        System.out.println("result"+result);
        return JsonResult.ok(result);
    }
    @ApiOperation(value="获取登录用户信息")
    @GetMapping("/login")
    public JsonResult<UserInfoVO> userInfo(String authToken){
        UserInfoVO userInfo=userInfoService.userInfo(authToken);
        return JsonResult.ok(userInfo);
    }
    @Deprecated
    @ApiOperation(value="验证用户认证数据")
    @GetMapping("/auth")
    public Authentication getAuthentication(Authentication authentication){
        return authentication;
    }


}
